package com.yequan.thread.monitor;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author yq
 * @date 2020/04/24
 */
public class MonitorDemo {

    private static ExecutorService es =
            new ThreadPoolExecutor(50, 100, 0L,
                    TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(100000));

    public static void main(String[] args) throws Exception {
        for (int i = 0; i < 100000; i++) {
            es.execute(() -> {
                System.out.print(1);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        }
        ThreadPoolExecutor tpe = ((ThreadPoolExecutor) es);
        while (true) {
            System.out.println();
            int queueSize = tpe.getQueue().size();
            System.out.println("当前排队任务数：" + queueSize);
            long completedTaskCount = tpe.getCompletedTaskCount();
            System.out.println("执行完成任务数：" + completedTaskCount);
            long taskCount = tpe.getTaskCount();
            System.out.println("总任务数：" + taskCount);
            int maximumPoolSize = tpe.getMaximumPoolSize();
            System.out.println("线程池允许的最大线程数：" + maximumPoolSize);
            int activeCount = tpe.getActiveCount();
            System.out.println("当前正在执行任务的线程数：" + activeCount);
            int poolSize = tpe.getPoolSize();
            System.out.println("当前线程池线程数量：" + poolSize);
            int largestPoolSize = tpe.getLargestPoolSize();
            System.out.println("线程池线程数量峰值：" + largestPoolSize);

            Thread.sleep(3000);
        }
    }

}
